﻿using Newtonsoft.Json;
using SaiPuLSCoffee.model;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;

namespace SaiPuLSCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.cfInit1 = porPortInfo.cfInit1;
                    IPorPortInfo.Instance.cfInit2 = porPortInfo.cfInit2;
                    IPorPortInfo.Instance.cfcxInit1 = porPortInfo.cfcxInit1;
                    IPorPortInfo.Instance.cfcxInit2 = porPortInfo.cfcxInit2;
                    IPorPortInfo.Instance.JBInit1 = porPortInfo.JBInit1;
                    IPorPortInfo.Instance.JBInit2 = porPortInfo.JBInit2;
                    IPorPortInfo.Instance.CoffeeJB = porPortInfo.CoffeeJB;
                }
                Thread thr = new Thread(SocketServer.Instance.CoffeeMake);
                thr.IsBackground = true;
                thr.Start();
                Thread thr1 = new Thread(IOMsg.Instance.IOSerial_Thr);
                thr1.IsBackground = true;
                thr1.Start();
                Thread thr2 = new Thread(Robot1Msg.Instance.IOSerial_Thr);
                thr2.IsBackground = true;
                thr2.Start();
                Thread thr3 = new Thread(Robot2Msg.Instance.IOSerial_Thr);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr4 = new Thread(IceMakingMsg.Instance.IOSerial_Thr);
                thr4.IsBackground = true;
                thr4.Start();
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 左臂流程
        /// </summary>
        public void MessageShowL()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (CoffeeMakes.Instance.coffeeInits.Count > 0)
                        {
                            if (CoffeeMakes.Instance.coffeeInits.Count == 1)
                            {
                                if (CoffeeMakes.Instance.coffeeInits.Any(a => a.ClassCodeInit != 4 && a.SWInit < 4))
                                {
                                    CoffeeMakes.Instance.MakingListL(CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit, CoffeeMakes.Instance.coffeeInits[0].ClassCodeInit, CoffeeMakes.Instance.coffeeInits[0].Guid);
                                }
                            }
                            else
                            {
                                if (CoffeeMakes.Instance.coffeeInits.Any(a => a.ClassCodeInit != 4 && a.LeachInit == 0))
                                {
                                    var cf = CoffeeMakes.Instance.coffeeInits.FirstOrDefault(a => a.ClassCodeInit != 4 && a.LeachInit == 0);
                                    CoffeeMakes.Instance.MakingListL(cf.TypeCodeInit, cf.ClassCodeInit, cf.Guid);
                                }
                                else
                                {
                                    var cf = CoffeeMakes.Instance.coffeeInits.FirstOrDefault(a => a.ClassCodeInit != 4);
                                    CoffeeMakes.Instance.MakingListL(cf.TypeCodeInit, cf.ClassCodeInit, cf.Guid);
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 右臂流程
        /// </summary>
        public void MessageShowR()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (CoffeeMakes.Instance.coffeeInits.Count > 0)
                        {
                            if (CoffeeMakes.Instance.coffeeInits.Count == 1)
                            {
                                if (CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit != 3)
                                {
                                    if (CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit == 1 && ((!CoffeeMakes.Instance.CFCCBol1 && WuLiaoCoffeeSel.Instance.Cup1_Sel_Bol) || (!CoffeeMakes.Instance.CFCCBol2 && WuLiaoCoffeeSel.Instance.Cup2_Sel_Bol)))
                                        CoffeeMakes.Instance.MakingListR(CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit, CoffeeMakes.Instance.coffeeInits[0].ClassCodeInit, CoffeeMakes.Instance.coffeeInits[0].Guid);
                                    else if (CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit == 2)
                                        CoffeeMakes.Instance.MakingListR(CoffeeMakes.Instance.coffeeInits[0].TypeCodeInit, CoffeeMakes.Instance.coffeeInits[0].ClassCodeInit, CoffeeMakes.Instance.coffeeInits[0].Guid);
                                }
                            }
                            else
                            {
                                if ((!CoffeeMakes.Instance.CFCCBol1 && WuLiaoCoffeeSel.Instance.Cup1_Sel_Bol) || (!CoffeeMakes.Instance.CFCCBol2 && WuLiaoCoffeeSel.Instance.Cup2_Sel_Bol))
                                {
                                    if (CoffeeMakes.Instance.coffeeInits.Any(a => a.TypeCodeInit == 1))
                                    {
                                        var cf = CoffeeMakes.Instance.coffeeInits.FirstOrDefault(a => a.TypeCodeInit == 1);
                                        CoffeeMakes.Instance.MakingListR(cf.TypeCodeInit, cf.ClassCodeInit, cf.Guid);
                                    }
                                    else
                                    {
                                        var cf = CoffeeMakes.Instance.coffeeInits.FirstOrDefault(a => a.TypeCodeInit == 2);
                                        CoffeeMakes.Instance.MakingListR(cf.TypeCodeInit, cf.ClassCodeInit, cf.Guid);
                                    }
                                }
                                else if (CoffeeMakes.Instance.coffeeInits.Any(a => a.TypeCodeInit == 2))
                                {
                                    var cf = CoffeeMakes.Instance.coffeeInits.FirstOrDefault(a => a.TypeCodeInit == 2);
                                    CoffeeMakes.Instance.MakingListR(cf.TypeCodeInit, cf.ClassCodeInit, cf.Guid);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
